<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Trade extends Controller_Admin_Main
{

    private $trade;
    private $where_arr = array();
    private $search_arr = array();

    public function before()
    {
        parent::before();
        $this->search_arr['keywords'] = '';
        $this->search_arr['category_id'] = SYSTEM_TRADE_CATEGORY;
        Assets::js('tinymce', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        $this->trade = Model::factory('trade_post');
    }

    //任务
    public function action_supply()
    {
        $this->where_arr['post_type'] = 'supply';
        $this->display_list();
    }

    //需求
    public function action_demand()
    {
        $this->where_arr['post_type'] = 'demand';
        $this->display_list();
    }

    public function display_list()
    {
        //获取供求分类
        $category_option = $this->get_category_cache_option('trade', $this->search_arr['category_id']);
        $this->template->assign('category_option', $category_option);
        $trade_list = $this->trade->get_trade_list($this->where_arr);
        $this->template->assign('post_type', $this->where_arr['post_type']);
        $this->template->assign('trade_list', $trade_list['list']);
        $this->template->assign('page', $trade_list['page']);
        $this->template->assign('search_arr', $this->search_arr);
        $this->template->view('trade_list');
    }

    //搜索相关
    public function action_search()
    {
        $category_id = Arr::get($_POST, 'category_id', SYSTEM_TRADE_CATEGORY);
        $category_arr = $this->get_category_childs($category_id, 'trade');
        $this->where_arr['category_id'] = count($category_arr) > 0 ? $category_arr : $category_id;
        $this->where_arr['post_type'] = Arr::get($_POST, 'post_type', 'supply');
        $this->where_arr['name'] = Arr::get($_POST, 'keywords', '');
        $this->search_arr['keywords'] = $this->where_arr['name'];
        $this->search_arr['category_id'] = $category_id;
        $this->display_list();
    }

    public function action_edit()
    {
        $id = $this->request->param('id');
        $trade_detail = $this->trade->get_trade_detail($id);
        //获取供求的推荐位
        $recommend_list = Model::factory('recommend')->get_recommends_id($id, 'trade');
        //获取推荐位类别
        $recommend_category = $this->get_category_cache_checkbox('recommend', SYSTEM_TRADE_RECOMMEND_CATEGORY, $recommend_list, 'recommend[]');
        $this->template->assign('checkboxs', $recommend_category);
        if ($_POST)
        {
            $recommend_arr = isset($_POST['recommend']) ? $_POST['recommend'] : array();
            if (is_array($recommend_arr))
            {
                Model::factory('recommend')->add_recommend($id, $recommend_arr, 'trade');
                Message::success(__('update success'));
                $this->request->redirect('admin/trade/edit/' . $id);
            }
        }
        $this->template->assign('data', $trade_detail);
        $this->template->view('trade_edit');
    }

    //删除供求以及对应的留言和附件
    public function action_delete()
    {
        $id = $this->request->param('id');
        if (!empty($id))
        {
            if ($this->trade->delete_trade($id))
            {
                Message::success(__('delete success'));
                $post_type = isset($_GET['type']) ? $_GET['type'] : 'supply';
                $this->request->redirect('admin/trade/' . $post_type);
            }
        }
    }

}